<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
                    <el-form-item :label="$locale('geWudevice.add.Experiment Name')" prop="experimentName">
                        <el-input v-model="queryParams.experimentName" :placeholder="$locale('geWuExperiment.Please enter the experiment name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('jobList.Submission time')">
                        <el-date-picker
                            v-model="dateRange"
                            style="width: 240px"
                            value-format="yyyy-MM-dd"
                            type="daterange"
                            range-separator="-"
                            :start-placeholder="$locale('classList.Start date')"
                            :end-placeholder="$locale('classList.End date')"></el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $locale('geWudevice.reset') }}</el-button>
                    </el-form-item>
                </el-form>

                <!-- <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" :disabled="ids.length == 0">新增</el-button>
                    </el-col>
                </el-row> -->

                <el-table v-loading="loading" :data="listData" @selection-change="handleSelectionChange">
                    <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
                    <el-table-column :label="$locale('geWudevice.add.Serial Number')" type="index" width="55"></el-table-column>
                    <el-table-column :label="$locale('geWudevice.add.Experiment Name')" align="center" key="experimentName" prop="experimentName"></el-table-column>
                    <el-table-column :label="$locale('teacher.add.Teacher Name')" align="center" key="teacherName" prop="teacherName"></el-table-column>
                    <el-table-column :label="$locale('jobList.add.email')" align="center" key="contactEmail" prop="contactEmail"></el-table-column>
                    <el-table-column :label="$locale('jobList.add.school')" align="center" key="schoolName" prop="schoolName"></el-table-column>
                    <el-table-column :label="$locale('jobList.Submission time')" align="center" key="experimentAdviseTime" prop="experimentAdviseTime"></el-table-column>
                    <el-table-column :label="$locale('order.add.Suggested content')" align="center" key="experimentAdvise" prop="experimentAdvise" show-overflow-tooltip>
                        <template slot-scope="scope">
                            <div v-html="scope.row.experimentAdvise"></div>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('formMarkering.list.operate')" align="center" width="160" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button> -->
                            <el-button size="mini" type="text" @click="handleUpdate(scope.row, true)">{{ $locale('geWudevice.view') }}</el-button>
                            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button> -->
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加 -->
        <addRecommendation ref="addRecommendation" @getList="getList"></addRecommendation>
    </div>
</template>

<script>
import { deleteById, list } from '@/api/recommendation';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import addRecommendation from './add/index.vue';
import { getNames } from '@/utils/index';

export default {
    name: 'recommendation',
    components: { Treeselect, addRecommendation },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            dateRange: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户表格数据
            listData: [],
            // 弹出层标题
            title: '',
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                experimentName: ''
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            if (this.dateRange.length > 0) {
                this.queryParams['startTime'] = this.dateRange[0];
                this.queryParams['endTime'] = this.dateRange[1];
            } else {
                this.queryParams['startTime'] = undefined;
                this.queryParams['endTime'] = undefined;
            }
            list(this.queryParams).then(({ data }) => {
                this.listData = data.records;
                this.total = data.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm('queryForm');
            this.queryParams.deptId = undefined;
            this.$refs.tree.setCurrentKey(null);
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$refs.addRecommendation.handleAdd(this.ids);
        },
        /** 修改按钮操作 */
        handleUpdate(row, disabled) {
            this.$refs.addRecommendation.show(row, disabled);
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const userIds = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除名称为"' + getNames(this.listData, userIds, 'experimentName') + '"的数据项？')
                .then(function () {
                    return deleteById({ id: userIds });
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        }
    }
};
</script>
<style lang="scss" scoped>
::v-deep .el-table__row--level-1 .el-checkbox {
    display: none;
}
</style>
